import string
from selenium import webdriver
import xlrd
import re
import time
import json
import openpyxl
from selenium.webdriver.chrome.options import Options
import random
from selenium.webdriver.common.keys import Keys
from pymouse import PyMouse
wb = openpyxl.Workbook()
ws = wb.create_sheet('sheet1')

class jim:
    k=3654
    m = PyMouse()
    def __init__(self):
        chrome_options = Options()
        chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        # self.browser = webdriver.Chrome()
        self.browser = webdriver.Chrome(options=chrome_options)
        self.bian=33600
    def slow_input(self,xpath, input_key, flag=0):
        for i in input_key:
            com1 = random.randint(1, 4)
            com2 = random.randint(1, 3)

            if (com1 == com2) and (flag == 0):
                num = random.randint(1, 3)
                str_list = [random.choice(string.digits + string.ascii_letters) for i in range(num)]
                random_str = ''.join(str_list)
                self.slow_input(xpath, random_str, flag=1)
            self.browser.find_element_by_xpath(xpath).send_keys(i)
            time.sleep(random.uniform(0.1, 0.25))
            if flag != 0:
                self.browser.find_element_by_xpath(xpath).send_keys(Keys.BACK_SPACE)
                time.sleep(random.uniform(0.1, 0.25))
            time.sleep(random.uniform(0.1, 0.25))

    def zidonghua(self):
        # 添加表头结构
        ws.cell(row=1, column=1).value = f'phone(prime)-1'
        ws.cell(row=1, column=2).value = f'phone-2'
        ws.cell(row=1, column=3).value = f'phone-3'
        ws.cell(row=1, column=4).value = f'phone-4'
        ws.cell(row=1, column=5).value = f'Current Address'
        ws.cell(row=1, column=6).value = f'Full Name'
        ws.cell(row=1, column=7).value = f'email-1(prime)'
        # ws.cell(row=1, column=8).value = f'email-2'
        # ws.cell(row=1, column=9).value = f'email-3'
        # ws.cell(row=1, column=10).value = f'email-4'




        # cookie = self.browser.get_cookies()  # 获取cookie
        # jsonCookies = json.dumps(cookie)
        # with open('storeCookie.json', 'w') as f:  # 创建文件保存cookie
        #     f.write(jsonCookies)
        # print(jsonCookies)

        for k1 in range(jim.k,self.bian):
            jim.k+=1
            if jim.k<self.bian:
                self.queqiao(k1)
                wb.save('changshi215.xlsx')
            else:
                print('已完成')
                break


    def req(self):
        list01 = []
        workbook = xlrd.open_workbook('C:\jim.xls')
        sheet2 = workbook.sheet_by_name('Sheet1')

        # jim.k-=1

        names04 = sheet2.cell(jim.k, 0)

        names02 = sheet2.cell(jim.k, 1)
        names03 = sheet2.cell(jim.k, 3)
        names = sheet2.cell(jim.k, 4)
        name01 = re.findall("text:'(.*?)'", str(names02))
        if name01!=[]:
        # name01 = re.findall("text:'(.*?)'", str(names02))[0]
            name01=name01[0]
            # name02 = re.findall("text:'(.*?)'", str(names03))[0]
            # name03 = re.findall("text:'(.*?)'", str(names))[0]
            # name04 = re.findall("text:'(.*?)'", str(names04))[0]
            name02 = str(names03)[6:-1]
            name03 = str(names)[6:-1]
            name04 = str(names04)[6:-1]
            zong1 = name01
            zong2=name02+','+name03
            zong3=name04
            zong=(zong1,zong2,zong3)
            list01.append(zong)

            print(list01)
            return list01
        else:
            return [('1','1','1','1')]

    def queqiao(self,k1):
        time.sleep(3)
        try:

            self.browser.get('https://www.fastpeoplesearch.com/address')

            time.sleep(2)
            # jim.m.click(1888,95)
            if self.browser.find_element_by_xpath(
                    "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                print('正在解决验证码')
                time.sleep(60)
                if self.browser.find_element_by_xpath(
                        "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                    time.sleep(60)

                    # m = PyMouse()
                    # proxylist
                    jim.m.click(180, 160)
                    time.sleep(1)
                    # 点击stopproxy
                    jim.m.click(930, 202)
                    time.sleep(1)

                    # 点击刷新
                    jim.m.click(825, 602)
                    # 点击proxyIP
                    time.sleep(20)
                    jim.m.click(124, 248)
                    time.sleep(20)

                    # 点击chrome
                    jim.m.click(37, 665)
                    time.sleep(15)

                    jim.k = k1
                    self.__init__()
                    self.zidonghua()
        except Exception as g:
            # try:

            time.sleep(3)
            # 模拟输入数据
            if self.req()[0][0]!='1':
                self.slow_input(xpath="//input[@id='search-address-1']", input_key=self.req()[0][0])
                print('000', jim.k)
                self.slow_input(xpath="//input[@id='search-address-2']", input_key=self.req()[0][1])
                print('111', jim.k)
                time.sleep(2)
                # self.browser.find_element_by_xpath("//input[@id='search-address-1']").send_keys(req()[k][0])
                # self.browser.find_element_by_xpath("//input[@id='search-address-2']").send_keys(req()[k][1])
                self.browser.find_element_by_xpath(
                    "//form[@id='form-search-address']/div[@class='search-form-buttons']/button[@class='search-form-button-submit btn btn-md btn-primary']").click()
                time.sleep(6)
                # # 点击关闭黑屏
                # jim.m.click(1882,118)
                # time.sleep(15)
                try:
                    self.browser.find_element_by_link_text(self.req()[0][2]).click()
                    time.sleep(6)
                    # 点击关闭黑屏
                    # jim.m.click(1882, 118)
                    # time.sleep(2)


                    #     获取Current Address：
                    try:
                        Current_Address = self.browser.find_element_by_xpath(
                            "//div[@id='current_address_section']/div[@class='detail-box-content']").text
                        print('Current_Address', Current_Address)
                        ws.cell(row=k1 + 1, column=5).value = f'{Current_Address}'
                    except Exception as a:
                        print('Current_Address获取有误')
                        ws.cell(row=k1 + 1, column=5).value = f'/'

                    #     电话获取：
                    #     主要电话：
                    try:
                        tel01 = self.browser.find_element_by_xpath(
                            "//div[@class='detail-box-phone']/div[@class='row']/dl[@class='col-sm-12 col-md-6'][1]/dt/h3/strong/a").text
                        print('tel01', tel01)
                        ws.cell(row=k1 + 1, column=1).value = f'{tel01}'
                    except Exception as g:
                        print('电话获取失败')
                        ws.cell(row=k1 + 1, column=1).value = f'/'
                    try:
                        #     次要电话：
                        tel02 = self.browser.find_elements_by_xpath(
                            "//div[@class='detail-box-phone']/div[@class='row']/dl[@class='col-sm-12 col-md-6']/dt/h3/a")
                        # print('tel02', tel02)
                        tel_i = 1

                        for i in tel02:
                            if i+1 < 5:
                                tel_i += 1
                                print(i.text)
                                ws.cell(row=k1 + 1, column=tel_i).value = f'{i.text}'
                    except Exception as g:
                        print('电话获取失败')
                        ws.cell(row=k1 + 1, column=1).value = f'/'
                    #     邮箱获取：
                    try:
                        email = self.browser.find_elements_by_xpath(
                            "//div[@id='email_section']/div[@class='detail-box-content']/div[@class='detail-box-email']/div[@class='row']/h3[@class='col-sm-12 col-md-6']")
                        # print(email)
                        email_j = 1
                        for j in range(len(email)):
                            email_j += 1
                            if j == 0:
                                # 主要邮箱：
                                print('主要邮箱', email[j].text)

                                ws.cell(row=k1 + 1, column=email_j + 5).value = f'{email[j].text}'
                            else:
                                print('次要邮箱', email[j].text)

                                ws.cell(row=k1 + 1, column=email_j + 5).value = f'{email[j].text}'
                                ws.cell(row=1, column=email_j + 5).value = f'email-{email_j-1}'
                    except Exception as g:
                        print('邮箱获取失败')
                        ws.cell(row=k1 + 1, column=7).value = f'/'
                    #     full name获取:
                    try:
                        full_name = self.browser.find_element_by_xpath(
                            "//div[@id='full_name_section']/h2/span[@class='fullname']").text
                        print('full_name', full_name)
                        ws.cell(row=k1 + 1, column=6).value = f'{full_name}'
                    except Exception as g:
                        print('full name获取失败')
                        ws.cell(row=k1 + 1, column=6).value = f'/'
                    time.sleep(6)
                    self.browser.back()
                    time.sleep(6)
                    self.browser.back()
                    time.sleep(6)
                except Exception as g:
                    try:
                        if self.browser.find_element_by_xpath(
                                "//form[@id='bot-check']/h1").text == 'Are you human?':
                            print('正在解决验证码2')
                            time.sleep(60)
                            if self.browser.find_element_by_xpath(
                                "//form[@id='bot-check']/h1").text == 'Are you human?':
                                time.sleep(60)
                                # m = PyMouse()
                                # proxylist
                                jim.m.click(180, 160)
                                time.sleep(1)
                                # 点击stopproxy
                                jim.m.click(930, 202)
                                time.sleep(1)

                                # 点击刷新
                                jim.m.click(825, 602)
                                # 点击proxyIP
                                time.sleep(20)
                                jim.m.click(124, 248)
                                time.sleep(20)

                                # 点击chrome
                                jim.m.click(37, 665)
                                time.sleep(15)
                                jim.k=k1
                                self.__init__()
                                self.zidonghua()
                    except Exception as g:
                        try:
                            if self.browser.find_element_by_xpath(
                                    "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                                print('正在解决验证码')
                                time.sleep(60)
                                if self.browser.find_element_by_xpath(
                                        "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                                    time.sleep(60)
                                    # m = PyMouse()
                                    # proxylist
                                    jim.m.click(180, 160)
                                    time.sleep(1)
                                    # 点击stopproxy
                                    jim.m.click(930, 202)
                                    time.sleep(1)

                                    # 点击刷新
                                    jim.m.click(825, 602)
                                    # 点击proxyIP
                                    time.sleep(20)
                                    jim.m.click(124, 248)
                                    time.sleep(20)

                                    # 点击chrome
                                    jim.m.click(37, 665)
                                    time.sleep(15)
                            jim.k = k1
                            self.__init__()
                            self.zidonghua()
                        except Exception as g:
                            print('客户信息暂无')
                            ws.cell(row=k1 + 1, column=1).value = f'/'
                            ws.cell(row=k1 + 1, column=5).value = f'/'
                            time.sleep(6)
                            self.browser.back()
                            time.sleep(6)

            else:
                print('1234567890')
                ws.cell(row=k1 + 1, column=1).value = f'null'
        time.sleep(20)





            # except Exception as g:
            #     print('网络中断')
            #     time.sleep(10)
            #     # m = PyMouse()
            #     # proxylist
            #     jim.m.click(176, 153)
            #     time.sleep(1)
            #     # 点击stopproxy
            #     jim.m.click(917, 191)
            #     time.sleep(1)
            #
            #     # 点击刷新
            #     jim.m.click(815, 595)
            #     # 点击proxyIP
            #     time.sleep(20)
            #     jim.m.click(120, 242)
            #     time.sleep(20)
            #     # # 点击program
            #     # m.click(80, 245)
            #     #
            #     # time.sleep(10)
            #     # 点击chrome
            #     jim.m.click(117, 638)
            #     time.sleep(15)
            #     # 点击jim
            #     if k1 % 2 == 0:
            #         jim.m.click(691, 402)
            #     else:
            #         jim.m.click(517, 385)
            #     time.sleep(3)
            #     jim.k = k1
            #     self.__init__()
            #     self.zidonghua()

if __name__ == '__main__':

    clear=jim()
    clear.zidonghua()



# try:
                        #     if self.browser.find_element_by_xpath(
                        #             "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                        #         print('正在解决验证码')
                        #         time.sleep(60)
                        #         if self.browser.find_element_by_xpath(
                        #                 "//div[@id='cf-error-details']/div[@class='cf-wrapper cf-header cf-error-overview']/h1").text == 'One more step':
                        #             time.sleep(10)
                        #             # m = PyMouse()
                        #             # proxylist
                        #             jim.m.click(176, 153)
                        #             time.sleep(1)
                        #             # 点击stopproxy
                        #             jim.m.click(917, 191)
                        #             time.sleep(1)
                        #
                        #             # 点击刷新
                        #             jim.m.click(815, 595)
                        #             # 点击proxyIP
                        #             time.sleep(20)
                        #             jim.m.click(120, 242)
                        #             time.sleep(20)
                        #             # # 点击program
                        #             # m.click(80, 245)
                        #             #
                        #             # time.sleep(10)
                        #             # 点击chrome
                        #             jim.m.click(117, 638)
                        #             time.sleep(15)
                        #             # 点击jim
                        #             jim.m.click(691, 402)
                        #         else:
                        #             try:
                        #                 self.browser.find_element_by_link_text(self.req()[0][2]).click()
                        #                 time.sleep(3)
                        #                 # 点击关闭黑屏
                        #                 jim.m.click(1340, 148)
                        #                 time.sleep(2)
                        #
                        #                 #     获取Current Address：
                        #                 try:
                        #                     Current_Address = self.browser.find_element_by_xpath(
                        #                         "//div[@id='current_address_section']/div[@class='detail-box-content']").text
                        #                     print('Current_Address', Current_Address)
                        #                     ws.cell(row=k1 + 1, column=5).value = f'{Current_Address}'
                        #                 except Exception as a:
                        #                     print('Current_Address获取有误')
                        #
                        #                 #     电话获取：
                        #                 #     主要电话：
                        #                 try:
                        #                     tel01 = self.browser.find_element_by_xpath(
                        #                         "//div[@class='detail-box-phone']/div[@class='row']/dl[@class='col-sm-12 col-md-6'][1]/dt/h3/strong/a").text
                        #                     print('tel01', tel01)
                        #                     ws.cell(row=k1 + 1, column=1).value = f'{tel01}'
                        #                     #     次要电话：
                        #                     tel02 = self.browser.find_elements_by_xpath(
                        #                         "//div[@class='detail-box-phone']/div[@class='row']/dl[@class='col-sm-12 col-md-6']/dt/h3/a")
                        #                     # print('tel02', tel02)
                        #                     tel_i = 1
                        #                     for i in tel02:
                        #                         tel_i += 1
                        #                         print(i.text)
                        #                         ws.cell(row=k1 + 1, column=tel_i).value = f'{i.text}'
                        #                 except Exception as g:
                        #                     print('电话获取失败')
                        #                 #     邮箱获取：
                        #                 try:
                        #                     email = self.browser.find_elements_by_xpath(
                        #                         "//div[@id='email_section']/div[@class='detail-box-content']/div[@class='detail-box-email']/div[@class='row']/h3[@class='col-sm-12 col-md-6']")
                        #                     # print(email)
                        #                     email_j = 1
                        #                     for j in range(len(email)):
                        #                         email_j += 1
                        #                         if j == 0:
                        #                             # 主要邮箱：
                        #                             print('主要邮箱', email[j].text)
                        #
                        #                             ws.cell(row=k1 + 1, column=email_j + 5).value = f'{email[j].text}'
                        #                         else:
                        #                             print('次要邮箱', email[j].text)
                        #
                        #                             ws.cell(row=k1 + 1, column=email_j + 5).value = f'{email[j].text}'
                        #                             ws.cell(row=1, column=email_j + 5).value = f'email-{email_j - 1}'
                        #                 except Exception as g:
                        #                     print('邮箱获取失败')
                        #                 #     full name获取:
                        #                 try:
                        #                     full_name = self.browser.find_element_by_xpath(
                        #                         "//div[@id='full_name_section']/h2/span[@class='fullname']").text
                        #                     print('full_name', full_name)
                        #                     ws.cell(row=k1 + 1, column=6).value = f'{full_name}'
                        #                 except Exception as g:
                        #                     print('full name获取失败')
                        #                 self.browser.back()
                        #                 time.sleep(2)
                        #                 self.browser.back()
                        #             except Exception as g:
                        #                 time.sleep(3)
                        #                 print('用户信息暂无')
                        #                 self.browser.back()
                        # except Exception as g:
                        #     time.sleep(10)
                        #     # m = PyMouse()
                        #     # proxylist
                        #     jim.m.click(176, 153)
                        #     time.sleep(1)
                        #     # 点击stopproxy
                        #     jim.m.click(917, 191)
                        #     time.sleep(1)
                        #
                        #     # 点击刷新
                        #     jim.m.click(815, 595)
                        #     # 点击proxyIP
                        #     time.sleep(20)
                        #     jim.m.click(120, 242)
                        #     time.sleep(20)
                        #     # # 点击program
                        #     # m.click(80, 245)
                        #     #
                        #     # time.sleep(10)
                        #     # 点击chrome
                        #     jim.m.click(117, 638)
                        #     time.sleep(15)
                        #     # 点击jim
                        #     jim.m.click(691, 402)
                        #     time.sleep(3)
                        #     jim.k = k1
                        #     self.__init__()
                        #     self.zidonghua()